// 系统相关工具函数
class SystemUtils {
    constructor() {
        // 获取状态栏高度
        this.statusBarHeight = 0
        this.navBarHeight = 0
        this.windowWidth = 0
        this.windowHeight = 0
        this.screenWidth = 0
        this.screenHeight = 0
        this.pixelRatio = 1
        this.safeArea = null
        this.safeAreaInsets = {
            top: 0,
            right: 0,
            bottom: 0,
            left: 0
        }
        this.init()
    }

    // 初始化系统信息
    init() {
        try {
            const systemInfo = uni.getSystemInfoSync()
            this.statusBarHeight = systemInfo.statusBarHeight || 0
            this.windowWidth = systemInfo.windowWidth || 0
            this.windowHeight = systemInfo.windowHeight || 0
            this.screenWidth = systemInfo.screenWidth || 0
            this.screenHeight = systemInfo.screenHeight || 0
            this.pixelRatio = systemInfo.pixelRatio || 1
            this.safeArea = systemInfo.safeArea || null

            // 获取安全区域 insets
            if (systemInfo.safeAreaInsets) {
                this.safeAreaInsets = systemInfo.safeAreaInsets
            } else if (this.safeArea) {
                // 兼容旧版本，手动计算 insets
                this.safeAreaInsets.top = this.safeArea.top
                this.safeAreaInsets.bottom = this.screenHeight - this.safeArea.bottom
                this.safeAreaInsets.left = this.safeArea.left
                this.safeAreaInsets.right = this.screenWidth - this.safeArea.right
            }

            // 计算导航栏高度（状态栏高度 + 导航栏高度）
            // 一般导航栏高度为 44px (iOS) 或 48px (Android)
            this.navBarHeight = this.statusBarHeight + (systemInfo.platform === 'ios' ? 44 : 48)
        } catch (error) {
            console.error('获取系统信息失败:', error)
            this.statusBarHeight = 0
            this.navBarHeight = 44
        }
    }

    // 获取状态栏高度
    getStatusBarHeight() {
        return this.statusBarHeight
    }

    // 获取导航栏高度
    getNavBarHeight() {
        return this.navBarHeight
    }

    // 获取窗口宽度
    getWindowWidth() {
        return this.windowWidth
    }

    // 获取窗口高度
    getWindowHeight() {
        return this.windowHeight
    }

    // 获取屏幕宽度
    getScreenWidth() {
        return this.screenWidth
    }

    // 获取屏幕高度
    getScreenHeight() {
        return this.screenHeight
    }

    // 获取像素比
    getPixelRatio() {
        return this.pixelRatio
    }

    // 获取安全区域信息
    getSafeArea() {
        return this.safeArea
    }

    // 获取安全区域 insets
    getSafeAreaInsets() {
        return this.safeAreaInsets
    }

    // 获取安全区域底部高度
    getSafeAreaBottom() {
        try {
            return this.safeAreaInsets.bottom || (this.safeArea?.bottom ? (this.screenHeight - this.safeArea.bottom) : 0)
        } catch (error) {
            console.error('获取安全区域底部高度失败:', error)
            return 0
        }
    }

    // 获取安全区域顶部高度
    getSafeAreaTop() {
        try {
            return this.safeAreaInsets.top || this.safeArea?.top || this.statusBarHeight
        } catch (error) {
            console.error('获取安全区域顶部高度失败:', error)
            return this.statusBarHeight
        }
    }

    // 获取安全区域左侧宽度
    getSafeAreaLeft() {
        return this.safeAreaInsets.left || (this.safeArea?.left || 0)
    }

    // 获取安全区域右侧宽度
    getSafeAreaRight() {
        return this.safeAreaInsets.right || (this.safeArea?.right ? (this.screenWidth - this.safeArea.right) : 0)
    }

    // 是否为 iPhone X 系列及以上机型
    isIPhoneX() {
        try {
            // iPhone X 系列的特征是存在安全区域且底部有安全距离
            return !!(this.safeArea && this.getSafeAreaBottom() > 0)
        } catch (error) {
            return false
        }
    }

    // 是否有刘海或挖孔
    hasNotch() {
        // 判断是否有刘海或挖孔屏幕
        return this.isIPhoneX() || this.safeAreaInsets.top > this.statusBarHeight
    }
}

// 创建单例实例
const systemUtils = new SystemUtils()

export default systemUtils